package com.grass.appointment.activity;

import android.os.IBinder;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.TextView;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import com.androidx.p013lv.base.bean.CityEntity;
import com.androidx.p013lv.base.bean.event.ChangeCityEvent;
import com.androidx.p013lv.base.http.BaseRes;
import com.androidx.p013lv.base.interfaces.Key;
import com.androidx.p013lv.base.listener.ItemClickListener;
import com.androidx.p013lv.base.p015ui.BaseActivity;
import com.androidx.p013lv.base.utils.SpUtils;
import com.androidx.p013lv.base.view.StatusControlLayout;
import com.google.gson.Gson;
import com.grass.appointment.C2694R;
import com.grass.appointment.adapter.CityListAdapter;
import com.grass.appointment.adapter.HotCityListAdapter;
import com.grass.appointment.adapter.SearchCityListAdapter;
import com.grass.appointment.bean.CityResBean;
import com.grass.appointment.bean.PinyinComparator;
import com.grass.appointment.databinding.ActivityCitySelectLayoutBinding;
import com.grass.appointment.model.CitySelectModel;
import com.grass.appointment.view.SideBar;
import com.gyf.immersionbar.ImmersionBar;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import org.greenrobot.eventbus.EventBus;

public class CitySelectActivity extends BaseActivity<ActivityCitySelectLayoutBinding> implements AbsListView.OnScrollListener, SideBar.OnTouchingLetterChangedListener {
    private HashMap<String, Integer> alphaIndexer;
    private AdapterView.OnItemClickListener cityItemClickListener = new AdapterView.OnItemClickListener() {
        /* class com.grass.appointment.activity.CitySelectActivity.C27042 */

        @Override // android.widget.AdapterView.OnItemClickListener
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
            EventBus.getDefault().post(new ChangeCityEvent(CitySelectActivity.this.type, CitySelectActivity.this.totalCityList.get(i - 1)));
            CitySelectActivity.this.finish();
        }
    };
    protected CityListAdapter cityListAdapter;
    private CityResBean cityResBean;
    protected List<CityEntity> hotCityList = new ArrayList();
    private HotCityListAdapter hotCityListAdapter;
    private ItemClickListener hotItemClickListener = new ItemClickListener() {
        /* class com.grass.appointment.activity.CitySelectActivity.C27031 */

        @Override // com.androidx.p013lv.base.listener.ItemClickListener
        public void onItemClick(View view, int i) {
            EventBus.getDefault().post(new ChangeCityEvent(CitySelectActivity.this.type, CitySelectActivity.this.hotCityList.get(i)));
            CitySelectActivity.this.finish();
        }
    };
    private boolean isScroll = false;
    private boolean mReady = false;
    private CitySelectModel model;
    private TextView overlay;
    private Runnable runnable = new Runnable() {
        /* class com.grass.appointment.activity.CitySelectActivity.RunnableC27064 */

        public void run() {
            CitySelectActivity.this.overlay.setVisibility(8);
        }
    };
    protected List<CityEntity> searchCityList = new ArrayList();
    protected SearchCityListAdapter searchCityListAdapter;
    private AdapterView.OnItemClickListener searchItemClickListener = new AdapterView.OnItemClickListener() {
        /* class com.grass.appointment.activity.CitySelectActivity.C27053 */

        @Override // android.widget.AdapterView.OnItemClickListener
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
            EventBus.getDefault().post(new ChangeCityEvent(CitySelectActivity.this.type, CitySelectActivity.this.searchCityList.get(i)));
            CitySelectActivity.this.finish();
        }
    };
    protected List<CityEntity> totalCityList = new ArrayList();
    public int type;
    private WindowManager windowManager;

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this).titleBar(((ActivityCitySelectLayoutBinding) this.binding).toolbar).init();
    }

    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public int setLayout() {
        return C2694R.C2699layout.activity_city_select_layout;
    }

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initView() {
        super.initView();
        ((ActivityCitySelectLayoutBinding) this.binding).setClick(new OnClick());
        getWindow().setSoftInputMode(2);
        this.alphaIndexer = new HashMap<>();
        this.model = (CitySelectModel) new ViewModelProvider(this).get(CitySelectModel.class);
        this.searchCityListAdapter = new SearchCityListAdapter(this);
        ((ActivityCitySelectLayoutBinding) this.binding).searchCityLv.setAdapter((ListAdapter) this.searchCityListAdapter);
        this.cityListAdapter = new CityListAdapter(this);
        ((ActivityCitySelectLayoutBinding) this.binding).status.setOnRetryListener(new View.OnClickListener() {
            /* class com.grass.appointment.activity.CitySelectActivity.View$OnClickListenerC27075 */

            public void onClick(View view) {
                ((ActivityCitySelectLayoutBinding) CitySelectActivity.this.binding).setStatus(2);
                CitySelectActivity.this.model.getCityList();
            }
        });
        ((ActivityCitySelectLayoutBinding) this.binding).totalCityLv.setAdapter((ListAdapter) this.cityListAdapter);
        initHeadView();
        ((ActivityCitySelectLayoutBinding) this.binding).totalCityLv.setOnScrollListener(this);
        ((ActivityCitySelectLayoutBinding) this.binding).totalCityLv.setOnItemClickListener(this.cityItemClickListener);
        ((ActivityCitySelectLayoutBinding) this.binding).searchCityLv.setOnItemClickListener(this.searchItemClickListener);
        ((ActivityCitySelectLayoutBinding) this.binding).sideBar.setOnTouchingLetterChangedListener(this);
        initOverlay();
        initListener();
        this.model.data().observe(this, new Observer<BaseRes<CityResBean>>() {
            /* class com.grass.appointment.activity.CitySelectActivity.C27086 */

            public void onChanged(BaseRes<CityResBean> baseRes) {
                ((ActivityCitySelectLayoutBinding) CitySelectActivity.this.binding).setStatus(0);
                if (baseRes.getCode() == 200) {
                    if (CitySelectActivity.this.cityResBean == null && baseRes.getData() == null) {
                        ((ActivityCitySelectLayoutBinding) CitySelectActivity.this.binding).setStatus(4);
                    } else if (baseRes.getData() != null) {
                        CitySelectActivity.this.cityResBean = baseRes.getData();
                        SpUtils.getInstance().put(Key.CITY, new Gson().toJson(CitySelectActivity.this.cityResBean));
                        CitySelectActivity citySelectActivity = CitySelectActivity.this;
                        citySelectActivity.initTotalCityList(citySelectActivity.cityResBean);
                    }
                } else if (CitySelectActivity.this.cityResBean == null) {
                    ((ActivityCitySelectLayoutBinding) CitySelectActivity.this.binding).setStatus(1);
                }
            }
        });
        CityResBean cityResBean2 = (CityResBean) SpUtils.getInstance().getBean(Key.CITY, CityResBean.class);
        this.cityResBean = cityResBean2;
        if (cityResBean2 == null) {
            ((ActivityCitySelectLayoutBinding) this.binding).setStatus(2);
        } else {
            initTotalCityList(cityResBean2);
        }
        this.model.getCityList();
    }

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initData() {
        super.initData();
    }

    private void initListener() {
        ((ActivityCitySelectLayoutBinding) this.binding).searchCityEdit.addTextChangedListener(new TextWatcher() {
            /* class com.grass.appointment.activity.CitySelectActivity.C27097 */

            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            public void afterTextChanged(Editable editable) {
                CitySelectActivity.this.setSearchCityList(((ActivityCitySelectLayoutBinding) CitySelectActivity.this.binding).searchCityEdit.getText().toString().trim().toLowerCase());
            }
        });
        ((ActivityCitySelectLayoutBinding) this.binding).searchCityEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            /* class com.grass.appointment.activity.CitySelectActivity.C27108 */

            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i != 3) {
                    return false;
                }
                CitySelectActivity citySelectActivity = CitySelectActivity.this;
                citySelectActivity.hideSoftInput(((ActivityCitySelectLayoutBinding) citySelectActivity.binding).searchCityEdit.getWindowToken());
                CitySelectActivity.this.setSearchCityList(((ActivityCitySelectLayoutBinding) CitySelectActivity.this.binding).searchCityEdit.getText().toString().trim().toLowerCase());
                return true;
            }
        });
    }

    private void initHeadView() {
        View inflate = View.inflate(this, C2694R.C2699layout.recent_city_item, null);
        HotCityListAdapter hotCityListAdapter2 = new HotCityListAdapter(this);
        this.hotCityListAdapter = hotCityListAdapter2;
        hotCityListAdapter2.setItemClickListener(this.hotItemClickListener);
        ((GridView) inflate.findViewById(C2694R.C2697id.recent_city_gv)).setAdapter((ListAdapter) this.hotCityListAdapter);
        ((ActivityCitySelectLayoutBinding) this.binding).totalCityLv.addHeaderView(inflate);
    }

    public void initTotalCityList(CityResBean cityResBean2) {
        this.hotCityList.clear();
        this.totalCityList.clear();
        this.hotCityList = cityResBean2.getListHot();
        this.totalCityList = cityResBean2.getListRegion();
        Collections.sort(this.totalCityList, new PinyinComparator());
        this.alphaIndexer.put("#", 0);
        for (int i = 0; i < this.totalCityList.size(); i++) {
            String upperCase = this.totalCityList.get(i).getInitial().toUpperCase();
            if (this.alphaIndexer.get(upperCase) == null) {
                this.alphaIndexer.put(upperCase, Integer.valueOf(i + 1));
            }
        }
        this.hotCityListAdapter.setCityEntities(this.hotCityList);
        this.cityListAdapter.setDataList(this.totalCityList);
    }

    private void initOverlay() {
        this.mReady = true;
        TextView textView = (TextView) LayoutInflater.from(this).inflate(C2694R.C2699layout.appointment_overlay, (ViewGroup) null);
        this.overlay = textView;
        textView.setVisibility(4);
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(-2, -2, 2, 524312, -3);
        WindowManager windowManager2 = (WindowManager) getSystemService("window");
        this.windowManager = windowManager2;
        if (windowManager2 != null) {
            windowManager2.addView(this.overlay, layoutParams);
        }
    }

    public void onScrollStateChanged(AbsListView absListView, int i) {
        boolean z = true;
        if (!(i == 1 || i == 2)) {
            z = false;
        }
        this.isScroll = z;
    }

    public void onScroll(AbsListView absListView, int i, int i2, int i3) {
        if (this.isScroll && this.mReady) {
            this.overlay.setText(i > 0 ? this.totalCityList.get(i - 1).getInitial().toUpperCase() : "#");
            this.overlay.setVisibility(0);
            this.overlay.removeCallbacks(this.runnable);
            this.overlay.postDelayed(this.runnable, 500);
        }
    }

    @Override // com.grass.appointment.view.SideBar.OnTouchingLetterChangedListener
    public void onTouchingLetterChanged(String str) {
        this.isScroll = false;
        if (this.alphaIndexer.get(str) != null) {
            ((ActivityCitySelectLayoutBinding) this.binding).totalCityLv.setSelection(this.alphaIndexer.get(str).intValue());
            this.overlay.setText(str);
            this.overlay.setVisibility(0);
            this.overlay.removeCallbacks(this.runnable);
            this.overlay.postDelayed(this.runnable, 500);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void setSearchCityList(String str) {
        this.searchCityList.clear();
        if (TextUtils.isEmpty(str)) {
            ((ActivityCitySelectLayoutBinding) this.binding).setIsSearch(0);
            return;
        }
        ((ActivityCitySelectLayoutBinding) this.binding).setIsSearch(1);
        for (int i = 0; i < this.totalCityList.size(); i++) {
            CityEntity cityEntity = this.totalCityList.get(i);
            if (cityEntity.getName().contains(str) || cityEntity.getPinyin().contains(str) || cityEntity.getInitial().contains(str) || cityEntity.getParentName().contains(str)) {
                this.searchCityList.add(cityEntity);
            }
        }
        if (this.searchCityList.size() == 0) {
            ((ActivityCitySelectLayoutBinding) this.binding).setIsSearch(2);
        }
        this.searchCityListAdapter.setCityEntities(this.searchCityList);
    }

    public void hideSoftInput(IBinder iBinder) {
        InputMethodManager inputMethodManager;
        if (iBinder != null && (inputMethodManager = (InputMethodManager) getSystemService("input_method")) != null) {
            inputMethodManager.hideSoftInputFromWindow(iBinder, 2);
        }
    }

    public static void showStatusView(StatusControlLayout statusControlLayout, int i) {
        statusControlLayout.showStatusView(i);
    }

    @Override // com.grass.appointment.view.SideBar.OnTouchingLetterChangedListener
    public void onTouchingLetterChanging(String str) {
        this.isScroll = false;
        if (this.alphaIndexer.get(str) != null) {
            ((ActivityCitySelectLayoutBinding) this.binding).totalCityLv.setSelection(this.alphaIndexer.get(str).intValue());
            this.overlay.setText(str);
            this.overlay.setVisibility(0);
            this.overlay.removeCallbacks(this.runnable);
            this.overlay.postDelayed(this.runnable, 500);
        }
    }

    public class OnClick {
        public OnClick() {
        }

        public void btnBack() {
            CitySelectActivity.this.finish();
        }
    }

    /* access modifiers changed from: protected */
    @Override // androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity, com.androidx.p013lv.base.p015ui.BaseActivity
    public void onDestroy() {
        this.overlay.removeCallbacks(this.runnable);
        WindowManager windowManager2 = this.windowManager;
        if (windowManager2 != null) {
            windowManager2.removeViewImmediate(this.overlay);
        }
        this.overlay = null;
        super.onDestroy();
    }
}
